﻿using CoreShop.Library;
using CoreShop.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CoreShop.App
{
    public class DingjianUtil
    {
        private string sFile = "";
        private corecmsscale scaleModel = new corecmsscale();
        private List<corecmsfoodspec> goodsList = new List<corecmsfoodspec>();

        /// <summary>
        /// 顶尖电子秤PLU
        /// </summary>
        /// <param name="IPaddr"></param>
        public void SendToScales_DJ(string IPaddr, corecmsscale _scaleModel, List<corecmsfoodspec> _goodsList)
        {
            this.goodsList = _goodsList;
            this.scaleModel = _scaleModel;
            //设置IP
            if (DingJian_SDK_SetBat(IPaddr) == false)
            {
                MessageBox.Show("设置IP参数失败！");
                return;
            }
            if (DingJian_SDK_PLU(IPaddr) == true)
            {
                DingJian_SDK_PLU_Run(IPaddr);
            }
        }

        private bool DingJian_SDK_SetBat(string IPaddr)
        {
            bool mResult = false;
            string sRowStr = "";
            string mPath = System.Windows.Forms.Application.StartupPath;
            try
            {
                //Clear PLU.bat
                sFile = mPath + "\\libs\\AclasSDK\\Clear PLU.bat";
                StreamWriter sw0 = new StreamWriter(sFile);
                sw0.Close();
                StreamWriter sw1 = new StreamWriter(sFile, false, Encoding.Default);
                sRowStr = @"AclasSDKConsole.exe -h " + '"' + IPaddr + '"' + " -t Del -b PLU -n \"*\" -f Ansi";
                sRowStr = sRowStr + "\r\n";
                sRowStr = sRowStr + "Pause";
                sw1.Write(sRowStr);
                sw1.Close();

                //DownLoad HotKey.bat
                sFile = System.Windows.Forms.Application.StartupPath + "\\libs\\AclasSDK\\DownLoad HotKey.bat";
                StreamWriter sw2 = new StreamWriter(sFile);
                sw2.Close();
                StreamWriter sw3 = new StreamWriter(sFile, false, Encoding.Default);
                sRowStr = @"AclasSDKConsole.exe -h " + '"' + IPaddr + '"' + " -t Down -b HotKey -n \"" + mPath + "\\libs\\AclasSDK\\HotKey.txt\" -f Ansi";
                sRowStr = sRowStr + "\r\n";
                sRowStr = sRowStr + "Pause";
                sw3.Write(sRowStr);
                sw3.Close();

                //DownLoad PLU.bat
                sFile = System.Windows.Forms.Application.StartupPath + "\\libs\\AclasSDK\\DownLoad PLU.bat";
                StreamWriter sw4 = new StreamWriter(sFile);
                sw4.Close();
                StreamWriter sw5 = new StreamWriter(sFile, false, Encoding.Default);
                sRowStr = @"AclasSDKConsole.exe -h " + '"' + IPaddr + '"' + " -t Down -b PLU -n \"" + mPath + "\\libs\\AclasSDK\\PLU.txt\" -f Ansi";
                sRowStr = sRowStr + "\r\n";
                sRowStr = sRowStr + "Pause";
                sw5.Write(sRowStr);
                sw5.Close();

                mResult = true;
            }

            catch (Exception ex)
            {
                mResult = false;
                MessageBox.Show("错误：" + ex.Message);
            }
            return mResult;
        }

        /// <summary>
        /// 顶尖电子秤PLU
        /// </summary>
        /// <param name="IPaddr"></param>
        private bool DingJian_SDK_PLU(string IPaddr)
        {
            bool bResult = false;
            sFile = System.Windows.Forms.Application.StartupPath + "\\libs\\AclasSDK\\PLU.txt";
            string sRowStr = "";
            //标题
            string sFielName = "ID	ItemCode	DepartmentID	GroupID	Name1	Name2	Name3	Price	UnitID	BarcodeType1	"
                        + "BarcodeType2	Label1ID	Label2ID	ProducedDate	FreshnessDate	ValidDate	PackageType	"
                        + "PackageWeight	PackagePrice	PackageRange	PackageDays	PackageHours	Text1ID	Text2ID	"
                        + "Text3ID	Text4ID	Text5ID	Text6ID	Text7ID	Text8ID	DiscountID	DiscountRate	HalfDiscount	"
                        + "QuarterDiscount	TareID	TareValue	IceID	ICEValue	OriginID	TraceabilityID	LimitPrice	"
                        + "Tax1	Tax2	Tax3	Tax4	Flag1	Flag2	Flag3	ProducedDateRule	FreshnessDateFrom	"
                        + "ValidDateFrom	PackageDateFrom	SpeedCode	Position1	SalesCategory	DiscountBeginTime	"
                        + "DiscountEndTime	DiscountPrice	DiscountFlag	" + "\r\n";

            string sRowFormat = "{0}	{1}	{2}	1	{3}			{4}	{5}	{6}	0	1	0	2014/9/22 15:06:00	"
                                + "0	0	0	{7}	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	0	"
                                + "0	0	0	0	0	1	0	0	0	0	0	0	0	0	0	0	0	0		0		"
                                + "1899/12/30	1899/12/30	0	0	";
            try
            {
                StreamWriter sw0 = new StreamWriter(sFile);
                sw0.Close();
                StreamWriter sw1 = new StreamWriter(sFile, false, Encoding.Default);

                string sTemp1 = "";
                string sTemp2 = "";
                string sItemID = "";
                string sItemCode = "";
                string sDepartmentID = "";
                string sName1 = "";
                string sPrice = "";
                string sUnitID = "";
                string sValidDate = "";
                //string BarcodeType1 = "99";//94为18位
                string BarcodeType1 = "2";//94为18位
                if (AppConstVars.systemsetup.weighingCodeType == (int)GlobalEnumVars.WeighingCodeType.CodeWeight13)
                {
                    BarcodeType1 = "7";//重量码
                }

                sw1.Write(sFielName);
                foreach (var item in goodsList)
                {
                    sItemID = item.weighPlu.ToString();
                    sTemp1 = item.number.ToString();
                    sItemCode = CommFunc.ConvertToInt32(sTemp1.Substring(2, sTemp1.Length - 2)).ToString();
                    sDepartmentID = sTemp1.Substring(0, 2);
                    sName1 = item.name;
                    sPrice = item.posPrice.ToString();
                    sUnitID = "4";
                    //if (CommFunc.ConvertDBNullToInt32(dr["FPriceMode"]) == 2) sUnitID = "10";//计件

                    sValidDate = item.periodDay.ToString();

                    sRowStr = string.Format(sRowFormat, sItemCode, sItemCode, sDepartmentID, sName1, sPrice, sUnitID, BarcodeType1, sValidDate);
                    sRowStr = sRowStr + "\r\n";
                    sw1.Write(sRowStr);
                }

                sw1.Close();
                bResult = true;

            }
            catch (Exception ex)
            {
                MessageBox.Show("错误：" + ex.Message);
            }
            return bResult;
        }

        /// <summary>
        /// 上传PU数据
        /// </summary>
        static void DingJian_SDK_PLU_Run(string IPADDR)
        {
            string mPath = System.Windows.Forms.Application.StartupPath;
            string[] arg = new string[1];
            arg[0] = @" -h " + '"' + IPADDR + '"' + " -t Down -b PLU -n \"" + mPath + "\\AclasSDK\\PLU.txt\" -f Ansi";

            CommFunc.StartProcess(Application.StartupPath + "\\libs\\AclasSDK\\AclasSDKConsole.exe", arg);
            Thread.Sleep(2000);
        }
    }
}
